/**
 * @file models/overview.js
 * @author
 */
import {routerRedux} from 'dva/router';
import api from '../api';

export default {
  namespace : 'income',
  state : {
  },
  subscriptions : {
    setup({dispatch, history}) {
      history.listen(({pathname, query}) => {
        if (pathname === '/investmentSummary/investDashboard'||pathname === '/achievement/equity/brinson'||pathname === '/investmentSummary/investDashboard') {
          dispatch({type: 'whetherRequest', payload: query})
        }
      })
    }
  },
  effects : {

    * whetherRequest({
      payload
    }, {put, select}) {
      yield put({type:'clearState'})
      const {lPortId, timeRange, cBenchCode, beginDate, endDate} = payload
      if (lPortId || timeRange || cBenchCode || beginDate || endDate) {
        if (lPortId && cBenchCode && ((timeRange == '0' && beginDate && endDate) || (timeRange != '0'))) {
          yield put({type: 'query', payload})
        }
      }
    },
    * query({
      payload
    }, {call, put, select}) {
      const {beginDate, cBenchCode, endDate, lPortId, timeRange} = payload

      const portfolio = yield call(api.getPortfolioratio, payload)


      yield put({
        type: 'querySuccess',
        payload: portfolio.data
      })

    },


  },
  reducers : {
    setState(state, {payload}) {
      return {
        ...state,
        ...payload
      }
    },

    // clearState(state) {
    //   return {
  
		// 	}
    // },

    querySuccess(state, {payload}) {

      return {
        ...state,
        ...payload
      }
    }
  }
};
